package com.heima.wemedia.service.impl;


import com.heima.model.comment.dtos.ApCommentDto;
import com.heima.model.comment.dtos.NewsCommentDto;
import com.heima.model.comment.pojos.ApComment;
import com.heima.model.comment.pojos.ApCommentRepay;
import com.heima.model.comment.vos.CommentRepayListVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.thread.WmThreadLocalUtils;
import com.heima.wemedia.service.CommentMangerService;
import com.heima.wemedia.service.WmSensitiveService;
import com.heima.wemedia.service.WmUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@Slf4j
@Transactional
public class CommentManagerServiceImpl implements CommentMangerService {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public ResponseResult list(NewsCommentDto dto) {
        if (dto ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        WmUser user = WmThreadLocalUtils.getUser();
        if (user == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        Query query = Query.query(Criteria.where("entryId").is(dto.getArticleId()));
//        Pageable page =  PageRequest.of(dto.getPage(),dto.getSize());
//        query.with(page);
        query.with(Sort.by(Sort.Direction.DESC, "createdTime"));
        List<ApComment> apCommentList = mongoTemplate.find(query, ApComment.class);

        List<CommentRepayListVo> commentRepayListVos = new ArrayList<>();
        for (ApComment apComment : apCommentList) {
            CommentRepayListVo commentRepayListVo = new CommentRepayListVo();
            commentRepayListVo.setApComments(apComment);
            List<ApCommentRepay> apCommentRepayList = mongoTemplate.find(Query.query(Criteria.where("commentId").is(apComment.getId())), ApCommentRepay.class);
            commentRepayListVo.setApCommentRepays(apCommentRepayList);
            commentRepayListVos.add(commentRepayListVo);
        }

        return ResponseResult.okResult(commentRepayListVos);
    }



    @Autowired
    private WmSensitiveService wmSensitiveService;

    @Autowired
    private WmUserService wmUserService;
    @Override
    public ResponseResult commentRepay(ApCommentDto dto) {
        if (dto ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        WmUser user =  WmThreadLocalUtils.getUser();
        if (user == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        if (dto.getContent().length() > 140) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "评论内容不能超过140字");
        }

//        try {
//            Map map = greenTextScan.greeTextScan(dto.getContent());
//            if (map != null){
//                return ResponseResult.errorResult(AppHttpCodeEnum.AUDIT_FAILED);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        if (!wmSensitiveService.handleSensitiveScan(dto.getContent())){
            return ResponseResult.errorResult(AppHttpCodeEnum.COMMENT_CONTAINS_OFFENDING_CONTENT,"评论含有违规内容");
        }


        WmUser uSer = wmUserService.getById(user.getId());

        //获取原评论数据
        Query query = Query.query(Criteria.where("id").is(dto.getCommentId()));
        //修改评论数+1
        ApComment apComment = mongoTemplate.findOne(query, ApComment.class);
        apComment.setReply(apComment.getReply()+1);
        mongoTemplate.findAndReplace(query,apComment);

        //评论回复表添加数据
        ApCommentRepay apCommentRepay = new ApCommentRepay();
        apCommentRepay.setAuthorId(user.getId());
        apCommentRepay.setAuthorName(uSer.getName());
        apCommentRepay.setContent(dto.getContent());
        apCommentRepay.setCreatedTime(new Date());
        apCommentRepay.setLikes(0);
        apCommentRepay.setUpdatedTime(new Date());
        apCommentRepay.setCommentId(Long.parseLong(dto.getCommentId()));

        mongoTemplate.save(apCommentRepay);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
